package com.bfly.mall.message.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.message.entity.Notify;
import com.bfly.mall.message.dto.NotifyFormDTO;
import com.bfly.mall.message.service.INotifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/11/1 10:37
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class NotifyServiceImpl extends BaseServiceImpl<Notify, Integer> implements INotifyService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(NotifyFormDTO formDTO) {
        Notify notify = new Notify();
        BeanUtils.copyProperties(formDTO, notify);

        notify.setCreateDate(new Date());
        notify.setRevoke(false);
        super.save(notify);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(NotifyFormDTO formDTO) {
        Notify dbNotify = super.get(formDTO.getId());
        if (dbNotify == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的系统通知信息!");
        }
        dbNotify.setTitle(formDTO.getTitle());
        dbNotify.setContent(formDTO.getContent());
        dbNotify.setUpdateDate(new Date());
        super.edit(dbNotify);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editNotifyRevoke(int notifyId, boolean isRevoke) {
        Notify dbNotify = super.get(notifyId);
        if (dbNotify == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的系统通知信息!");
        }

        dbNotify.setRevoke(isRevoke);
        if(isRevoke) {
            dbNotify.setRevokeDate(new Date());
        }else{
            dbNotify.setRevokeDate(null);
        }
        dbNotify.setUpdateDate(dbNotify.getRevokeDate());
        super.edit(dbNotify);
    }
}
